package com.ai.bitcoin.schedule.task;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccBatch;
import com.ai.bitcoin.jpa.entity.BccIndexInfo;
import com.ai.bitcoin.jpa.entity.BccIndexInfoCrawl;
import com.ai.bitcoin.jpa.entity.BccIndexInfoHis;
import com.ai.bitcoin.jpa.service.*;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.thread.pool.TaskExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jason
 * @date 2018/4/6
 **/
@Slf4j
@Component
public class IndexTask extends AbstractTask {
    @Autowired
    BccPriceInfoService bccPriceInfoService;
    @Autowired
    BccPriceDayService bccPriceDayService;
    @Autowired
    BccBatchService bccBatchService;
    @Autowired
    BccBasicInfoService bccBasicInfoService;
    @Autowired
    BccPriceInfoCrawlService bccPriceInfoCrawlService;
    @Autowired
    BccRedisService bccRedisService;
    @Autowired
    BccShowControlService bccShowControlService;
    @Autowired
    TaskExecutor taskExecutor;
    @Autowired
    BccIndexInfoCrawlService bccIndexInfoCrawlService;
    @Autowired
    BccIndexInfoService bccIndexInfoService;
    @Autowired
    BccIndexInfoHisService bccIndexInfoHisService;


    @Override
    public void execute() {
        long start = System.currentTimeMillis();
        List<BccBatch> indexBatchList = bccBatchService.findByBatchTypeAndBatchStateOrderByBatchTimeEndDesc(Bcc.BathType.CRAWL_INDEX_INFO, Bcc.BathState.CRAWL_DONE);
        if (indexBatchList.size() == 0) {
            log.info("没有要处理的新信息，不需要重新计算");
            return;
        }

        BccBatch indexBatch = indexBatchList.get(0);
        String indexBatchNo = indexBatch.getBatchNo();
        Sort sort = new Sort(Sort.Direction.ASC, "bccId");
        Pageable pageable = PageRequest.of(0, pageSizeDb, sort);
        Page<BccIndexInfoCrawl> page = bccIndexInfoCrawlService.findByBatchNo(indexBatchNo, pageable);
        List<BccIndexInfoCrawl> list = page.getContent();

        int pageNum = page.getTotalPages();
        log.info("总共页数:{},每页大小:{}", pageNum, pageSizeDb);

        if (pageNum > 0) {
            log.info("清理未发布的脏数据");
            bccIndexInfoService.deleteNoPublish();
        }

        for (int i = 0; i < pageNum; i++) {
            log.info("第{}页", i);
            if (i != 0) {
                pageable = PageRequest.of(i, pageSizeDb, sort);
                page = bccIndexInfoCrawlService.findByBatchNo(indexBatchNo, pageable);
                list = page.getContent();
            }
            for (int i1 = 0; i1 < list.size(); i1++) {
                BccIndexInfo bccIndexInfo = new BccIndexInfo();
                BeanUtils.copyProperties(list.get(i), bccIndexInfo);
                bccIndexInfoService.save(bccIndexInfo);
            }
        }

        log.info("save to bcc_index_info_his");
        sort = new Sort(Sort.Direction.ASC, "bccId");
        pageable = PageRequest.of(0, pageSizeDb, sort);
        Page<BccIndexInfo> indexPage = bccIndexInfoService.findByRecordState(Bcc.RecordState.PUBLISH, pageable);
        List<BccIndexInfo> indexInfoList = indexPage.getContent();
        pageNum = indexPage.getTotalPages();
        for (int i = 0; i < pageNum; i++) {
            if (i != 0) {
                PageRequest.of(0, pageSizeDb, sort);
                indexPage = bccIndexInfoService.findByRecordState(Bcc.RecordState.PUBLISH, pageable);
                indexInfoList = indexPage.getContent();
            }
            for (BccIndexInfo anIndexInfoList : indexInfoList) {
                save2IndexHis(anIndexInfoList);
            }
        }

        log.info("换版bcc_rank,batchNo:{}", indexBatchNo);
        bccIndexInfoService.updateRank(indexBatchNo);
        log.info("将数据copy入 bcc_index_info,耗时:{}", (System.currentTimeMillis() - start));
        bccBatchService.save(indexBatch);
    }

    private void save2IndexHis(BccIndexInfo bccIndexInfo) {
        BccIndexInfoHis bccIndexInfoHis = new BccIndexInfoHis();
        BeanUtils.copyProperties(bccIndexInfo, bccIndexInfoHis);
        bccIndexInfoHisService.save(bccIndexInfoHis);
    }
}
